package com.community;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.forever.common.MyUtil;
import com.forever.common.dao.CommonDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class EventAction extends ActionSupport implements Preparable, ModelDriven<EventDTO>{

	private static final long serialVersionUID = 1L;
	
	private EventDTO dto;

	@Resource(name="dao")
	private CommonDAO dao;
	
	@Resource(name="myUtil")
	private MyUtil myUtil;
	
	
	@Override
	public EventDTO getModel() {
		
		return dto;
	}

	@Override
	public void prepare() throws Exception {
		
		dto = new EventDTO();
		
	}
	
	public String e_created() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		if(dto == null || dto.getMode() == null || dto.getMode().equals("")){
			request.setAttribute("mode", "created");
			return INPUT;
		}
		
		int numMax = dao.getIntValue("event.numMax");
		dto.setNum(numMax +1);
		
		dao.insertData("event.insertData", dto);	
	
		return SUCCESS;
	}
	
	public String e_list() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String cp = request.getContextPath();
		
		int numPerPage = 10;
		int totalPage = 0;
		int totalDataCount = 0;
		
		String pageNum = request.getParameter("pageNum");
		
		int currentPage = 1;

		if(pageNum != null ){
			currentPage = Integer.parseInt(pageNum);
		}
		
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(searchKey == null){
			
			searchKey = "subject";
			searchValue = "";
		}
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("searchKey", searchKey);
		hMap.put("searchValue", searchValue);
		
		totalDataCount = dao.getIntValue("event.dataCount", hMap);
		
		if(totalDataCount != 0)
			totalPage = myUtil.getPageCount(numPerPage, totalDataCount);
		
		if(currentPage > totalPage)
			currentPage = totalPage;
		
		int start = (currentPage-1)*numPerPage+1;
		int end = currentPage*numPerPage;
		
		hMap.put("start", start);
		hMap.put("end", end);
		
		List<Object> lists = (List<Object>)dao.getListData("event.listData", hMap);
		
		int listNum, n = 0;
		Iterator<Object> it = lists.iterator();
		
		while(it.hasNext()){
			EventDTO vo = (EventDTO)it.next();
			listNum = totalDataCount - (start + n - 1);
			vo.setListNum(listNum);
			n++;
		}
		
		
		String params = "";
		String urlArticle = "";
		String urlList = "";
		
		if(!searchValue.equals("")){
			searchValue = URLEncoder.encode(searchValue, "UTF-8");
			params = "searchKey=" +searchKey;
			params+= "&searchValue=" +searchValue;
		}
		
		urlList = cp + "/community/e_list.action";
		urlArticle = cp + "/community/e_article.action?pageNum=" +currentPage;
		
		if(!params.equals("")){
			
			urlList += "?" + params;
			urlArticle += "&" +params;
		}
		
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("lists", lists);
		request.setAttribute("totalDataCount", totalDataCount);
		request.setAttribute("urlArticle", urlArticle);
		request.setAttribute("pageNum", currentPage);
		request.setAttribute("pageIndexList", myUtil.pageIndexList(currentPage, totalPage, urlList));
		
		return SUCCESS;
	}
	
	public String e_article() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		int num = Integer.parseInt(request.getParameter("num"));
		String pageNum = request.getParameter("pageNum");
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(searchKey == null){
			searchKey = "subject";
			searchValue = "";
		}
		
		if(request.getMethod().equalsIgnoreCase("GET"))
			searchValue = URLDecoder.decode(searchValue,"UTF-8");
		
		dao.updateData("event.updateHitCount", num);
		
		dto = (EventDTO)dao.getReadData("event.readData", num);
		
		if(dto == null)
			return "read-error";
		
		dto.setContent(dto.getContent().replaceAll("\r\n", "<br/>"));
		
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("searchKey", searchKey);
		hMap.put("searchValue", searchValue);
		hMap.put("num", dto.getNum());
		
		EventDTO preDTO = (EventDTO)dao.getReadData("event.preReadData", hMap);
		int preNum = 0;
		String preSubject = "";
		if(preDTO != null){
			
			preNum = preDTO.getNum();
			preSubject = preDTO.getSubject();
		}
		
		EventDTO nextDTO = (EventDTO)dao.getReadData("event.nextReadData", hMap);
		int nextNum = 0;
		String nextSubject = "";
		if(nextDTO != null){
			
			nextNum = nextDTO.getNum();
			nextSubject = nextDTO.getSubject();
		}
		
		String params = "pageNum=" +pageNum;
		String search = "";
		if(!searchValue.equals("")){
			
			searchValue = URLEncoder.encode(searchValue,"UTF-8");
			params += "&searchKey=" +searchKey;
			params += "&searchValue=" +searchValue;
			search += "&searchKey=" +searchKey;
			search += "&searchValue=" +searchValue;
		}
	
		request.setAttribute("dto", dto);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("preNum", preNum);
		request.setAttribute("preSubject", preSubject);
		request.setAttribute("nextNum", nextNum);   
		request.setAttribute("nextSubject", nextSubject);
		request.setAttribute("params", params);
		request.setAttribute("search", search);
		
		return SUCCESS;
		
	}
	
	
	public String e_updated() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		int num = Integer.parseInt(request.getParameter("num"));	
		String pageNum = request.getParameter("pageNum");
		String searchKey = request.getParameter("searchKey");
		String searchValue = request.getParameter("searchValue");
		
		if(dto == null || dto.getMode() == null || dto.getMode().equals("")){	
			
			dto = (EventDTO)dao.getReadData("event.readData", num);
			
			request.setAttribute("dto", dto);
			request.setAttribute("num", num);
			request.setAttribute("pageNum", pageNum);
			request.setAttribute("searchKey", searchKey);
			request.setAttribute("searchValue", searchValue);			
			request.setAttribute("mode", "updated");
			
			System.out.println("pageNum : "+pageNum);
			System.out.println("searchKey : "+searchKey);
			System.out.println("searchValue : " +searchValue);
			return INPUT;
		}
			
		if(searchKey == null){
			searchKey = "subject";
			searchValue = "";
		}
		
		if(request.getMethod().equalsIgnoreCase("GET"))
			searchValue = URLDecoder.decode(searchValue,"UTF-8");
		
		dao.updateData("event.updateData", dto);
		
		dto.setPageNum(pageNum);
		dto.setSearchKey(searchKey);
		dto.setSearchValue(searchValue);
		
		return SUCCESS;
	}
	
	public String e_deleted() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		int num = Integer.parseInt(request.getParameter("num"));
		
		dao.deleteData("event.deleteData", num);
		
		return SUCCESS;
		
	}
	

}
